package cc.rengu.igas.channel.upqc.core.service.trans;


import cc.rengu.igas.channel.upqc.common.constant.UpqcParamConstant;
import cc.rengu.igas.channel.upqc.common.constant.UpqcTreeNodeConstant;
import cc.rengu.igas.channel.upqc.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upqc.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upqc.core.service.base.PayService;
import cc.rengu.igas.channel.upqc.facade.bean.PayeeInfoBean;
import cc.rengu.igas.channel.upqc.facade.request.B2CQueryOrderRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CQueryOrderResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.net.URLEncoder;

/*
 * 付款方订单查询
 * reqType:0120000903
 * IS->UP
 * */

public class B2cQueryOrderService extends PayService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        B2CQueryOrderRequest b2CQueryOrderRequest = new B2CQueryOrderRequest();
        ConvertUtil.convertOutput(b2CQueryOrderRequest);
        return b2CQueryOrderRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpqcTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        B2CQueryOrderResponse b2CQueryOrderResponse = new B2CQueryOrderResponse();
        B2CQueryOrderRequest b2CQueryOrderRequest = (B2CQueryOrderRequest) request;
        /* 根据微信支付产品类型对接口接口字段赋值 */
        ConvertUtil.convertInput(b2CQueryOrderRequest);
        /* 根据通道标识调用不同通道进行支付 */
        xmlTreeUtil.setXmlTreeStringValue(UpqcTreeNodeConstant.txnType, b2CQueryOrderRequest.getHeader().getTxnNum());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, b2CQueryOrderRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        /* 恢复原交易码 */
        if (98 == iReturnCode) {
            rglog.error("调用银联二维码服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.TIME_OUT_ERROR.getRespCode(), RespCodeEnum.TIME_OUT_ERROR.getRespDesc());
        } else if (0 != iReturnCode) {
            rglog.error("调用银联二维码服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        /*根据银联返回报文组装*/
        ConvertUtil.convertOutput(b2CQueryOrderResponse);
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        if (UpqcParamConstant.SUCCESS_CODE.equals(b2CQueryOrderResponse.getRespCode())) {
            /*转换交易码*/
            b2CQueryOrderResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            b2CQueryOrderResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        }
        /*解密收款方信息*/
        String payeeInfoEnc = xmlTreeUtil.getXmlTreeStringValue(UpqcTreeNodeConstant.payeeInfoStr);
        if (payeeInfoEnc != null) {
            String payeeInfo = descryptData(payeeInfoEnc);
            rglog.error("解密的payInfo:<{}>", payeeInfo);
            b2CQueryOrderResponse.setPayeeInfoBean((PayeeInfoBean) convertKvStr2Bean(payeeInfo.substring(1, payeeInfo.length() - 1), PayeeInfoBean.class));
        }
        chnlBizResponse.setResult(b2CQueryOrderResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        B2CQueryOrderResponse b2CQueryOrderResponse = (B2CQueryOrderResponse) response.getResult();
        ConvertUtil.convertInput(b2CQueryOrderResponse);
    }

}
